import com.henry.bean.ClassNameUtils;
import com.henry.bean.ColumnEntity;
import com.henry.bean.TableEntity;
import org.apache.commons.lang.StringUtils;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.io.StringWriter;
import java.util.*;
import java.util.zip.ZipEntry;

/**
 * Created by 峥桂 on 2017/9/21.
 */
public class XlsJava {
    static VelocityEngine ve = new VelocityEngine();

    private TableEntity tableEntity;
    static VelocityContext context ;
//    private final static String TEMPLATE_PATH = "src/main/resources/template_ftl";
    private final static String TEMPLATE_PATH = XlsJava.class.getResource("/").getFile().toString()+"/template_ftl";
//    @PostConstruct
    private static void initData() {
//        XlsJava.class.getResource("/").getFile().toString()
        System.out.println( XlsJava.class.getResource("/").getFile().toString());
        //设置velocity资源加载器
        Properties prop = new Properties();
        prop.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, TEMPLATE_PATH); //此处的fileDir可以直接用绝对路径来
        ve.init(prop);
    }
//    @PostConstruct
    private static void setVelocityContext() {
        //封装模板数据
        Map<String, Object> map = new HashMap<>();
        map.put("tableName", "A");
        map.put("comments", "B");
        map.put("pk", "B");
        map.put("className", "B");
        map.put("classname", "B");
        map.put("pathName", "B");
        map.put("columns", "B");
        map.put("package", "B");
        map.put("customerName", "B");
        map.put("author","B");
        map.put("email", "B");
        map.put("datetime", "B");
        context = new VelocityContext(map);
    }
    private Map<String, String> columns;
    private void setTableEntity() {
        //表信息
        tableEntity = new TableEntity();
        tableEntity.setTableName("tableName");
        tableEntity.setComments("tableComment");
        //表名转换成Java类名 packageConfig
        String className = ClassNameUtils.tableToJava(tableEntity.getTableName(), "TablePrefix");
        tableEntity.setClassName(className);
        tableEntity.setClassname(StringUtils.uncapitalize(className));

        //列信息
        ColumnEntity columnEntity;
        List<ColumnEntity> columsList = new ArrayList<>();

        for (String key : columns.keySet()) {
            columnEntity = new ColumnEntity();
//            columnEntity.setDataType(column.get("dataType"));
//            columnEntity.setColumnName(column.get("columnName"));
//            columnEntity.setComments(column.get("columnComment"));
//            columnEntity.setAttrLength(ClassNameUtils.getColunmLength(column.get("columnTypeStr")));
//            columnEntity.setNullAble(column.get("nullAble"));
            //列名转换成Java属性名
            String attrName = ClassNameUtils.columnToJava(columnEntity.getColumnName());
            columnEntity.setAttrName(attrName);
            columnEntity.setAttrname(StringUtils.uncapitalize(attrName));
            //列的数据类型，转换成Java类型
            String attrType = ClassNameUtils.dataTypeConvertMap.get(columnEntity.getDataType());
            attrType = (StringUtils.isBlank(attrType) ? "unknowType" : attrType);
            columnEntity.setAttrType(attrType);


            columsList.add(columnEntity);
        }
        tableEntity.setColumns(columsList);
        //没主键，则第一个字段为主键
        if (tableEntity.getPk() == null) {
            tableEntity.setPk(tableEntity.getColumns().get(0));
        }
    }

    public static void main(String[] args) {
        initData();
        setVelocityContext();
        //渲染模板
        StringWriter sw = new StringWriter();
        Template tpl = ve.getTemplate("entity.java.vm", "UTF-8");
        tpl.merge(context, sw);
        System.out.println(sw.toString());
//        try {
//            String packagename = ClassNameUtils.getFileName(template, tableEntity.getClassName(),
//                    packageConfig.getPackageName(), packageConfig.getCustomerName());
//            //添加到zip
//            zip.putNextEntry(new ZipEntry(packagename));
//            IOUtils.write(sw.toString(), zip, "UTF-8");
//            IOUtils.closeQuietly(sw);
//            zip.closeEntry();
//        } catch (IOException e) {
//            throw new RuntimeException("渲染模板失败，表名：" + tableEntity.getTableName(), e);
//        }
    }
}
